package com.swgk.core.util;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;
import com.swgk.core.event.EventConstant;

public class GoodsTimerUtils extends CountDownTimer {
    private TextView day; //天
    private TextView tv_day; //天数
    private TextView tv_hours; //小时数
    private TextView tv_minutes; //分钟数
    private TextView tv_seconds; //秒数
    private int position;//选中的sku下标

    /**
     * @param day        The 天
     * @param tv_day     The 天数
     * @param tv_hours   The 小时数
     * @param tv_minutes The 分钟数
     * @param tv_seconds The 秒数
     * @param endTime    millisInFuture  从开始调用start()到倒计时完成
     */
    public GoodsTimerUtils(long endTime, TextView day, TextView tv_day,
                           TextView tv_hours, TextView tv_minutes, TextView tv_seconds,int position) {
        super(endTime, 1000);
        this.day = day;
        this.tv_day = tv_day;
        this.tv_hours = tv_hours;
        this.tv_minutes = tv_minutes;
        this.tv_seconds = tv_seconds;
        this.position = position;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        long dd = millisUntilFinished / 1000 / 86400;//天
        long hh = millisUntilFinished / 1000 % 86400 / 3600;                 //转换小时
        long mm = millisUntilFinished / 1000 % 86400 % 3600 / 60;            //转换分钟
        long ss = millisUntilFinished / 1000 % 86400 % 3600 % 60;            //剩余秒数
        if (dd > 0) {
            day.setVisibility(View.VISIBLE);
            tv_day.setVisibility(View.VISIBLE);
            tv_day.setText(String.valueOf(dd));
        } else {
            day.setVisibility(View.GONE);
            tv_day.setVisibility(View.GONE);
        }
        tv_hours.setText(String.valueOf(hh).length() == 1 ? "0" + hh : String.valueOf(hh));
        tv_minutes.setText(String.valueOf(mm).length() == 1 ? "0" + mm : String.valueOf(mm));
        tv_seconds.setText(String.valueOf(ss).length() == 1 ? "0" + ss : String.valueOf(ss));
    }

    @Override
    public void onFinish() {
        UiUtil.sendMsg(EventConstant.EVENT_REFRESH_SHOP_DETAIL, position);
    }
}